#include "uart_protocal.h"
#include "ab32_uart.h"
#include <stdio.h>
#include <stdlib.h>
#include "board.h"
/*
通信协议制定
以 AA 开头
以 55 结尾

MCU-->8266 [AA addr data1 data2 check 55]

8266-->MCU [55 addr data1 data2 check AA]

addr:地址
00 获取版本号     [muc <-- 8266]
01 灯            [mcu <-- 8266]
02 风扇          [mcu <-- 8266]
03 加热片        [mcu <-- 8266]
04 加湿器        [mcu <-- 8266]

10 温度         [mcu --> 8266]
11 湿度         [mcu --> 8266]
12 光线         [mcu --> 8266]


data

00 : 获取版本号
data1 56
data2 58

*/
#define AB_RECV_MAX_LEN (16)
#define AB_SEND_MAX_LEN (8)
rt_uint8_t ab_rx_buff[AB_RECV_MAX_LEN];
rt_uint8_t ab_tx_buff[AB_SEND_MAX_LEN];

static   rt_uint32_t version_val = 0;   //版本号值
static   rt_uint32_t led_val = 0;       //led的值
static   rt_uint32_t fan_val = 0;       //风扇的值
static   rt_uint32_t ptc_val = 0;       //加热片的值
static   rt_uint32_t humidifier_val = 0;    //加湿器的值

typedef rt_uint8_t(*CbMsgProc)(rt_uint8_t, rt_uint8_t);

typedef struct
{
    rt_uint8_t cmdCode;
    CbMsgProc msgProc;
} ESP_Send_MsgEntry;

typedef struct
{
    rt_uint8_t cmdCode;
    CbMsgProc msgProc;
} AB_Send_MsgEntry;

rt_uint8_t get_ctrl_val(ESP_SEND_CMD_T cmd_code, rt_uint32_t *ctrl_val)
{
    rt_uint8_t ret = 0;
    switch(cmd_code)
    {
        case 0: //版本号
            if(version_val >> 8)
            {
                *ctrl_val = (version_val & 0xff);
                ret = 1;
            }
            version_val = 0;
            break;
        case 1: //LED
            if(led_val >> 8)
            {
                *ctrl_val = (led_val & 0xff);
                ret = 1;
            }
            led_val = 0;
            break;
        case 2: //风扇
            if(fan_val >> 8)
            {
                *ctrl_val = (fan_val & 0xff);
                ret = 1;
            }
            fan_val = 0;
            break;
        case 3: //加热片
            if(ptc_val >> 8)
            {
                *ctrl_val = (ptc_val & 0xff);
                ret = 1;
            }
            ptc_val = 0;
            break;
        case 4: //加湿器
            if(humidifier_val >> 8)
            {
                *ctrl_val = (humidifier_val & 0xff);
                ret = 1;
            }
            humidifier_val = 0;
            break;
        default:
            break;
    }
    return ret;
}

void raw_pack_init(rt_uint8_t code, rt_uint8_t data1, rt_uint8_t data2)
{
    ab_tx_buff[0] = 0xAA;
    ab_tx_buff[1] = code;
    ab_tx_buff[2] = data1;
    ab_tx_buff[3] = data2;
    ab_tx_buff[4] = (rt_uint8_t)(data1 + data2);
    ab_tx_buff[5] = 0x55;
}

//发送版本号
rt_uint8_t esp_send_version(rt_uint8_t data1, rt_uint8_t data2)
{
    version_val = (((data1 + 1) << 8) | data2);
    return 0;
}

//发送LED状态
rt_uint8_t esp_send_led_state(rt_uint8_t data1, rt_uint8_t data2)
{
    led_val = (((data1 + 1) << 8) | data2);
    return 0;
}

//发送风扇状态
rt_uint8_t esp_send_fan_val(rt_uint8_t data1, rt_uint8_t data2)
{
    fan_val = (((data1 + 1) << 8) | data2);
    return 0;
}

//发送加热片状态
rt_uint8_t esp_send_ptc_val(rt_uint8_t data1, rt_uint8_t data2)
{
    ptc_val = (((data1 + 1) << 8) | data2);
    return 0;
}

//发送加湿器状态
rt_uint8_t esp_send_humidifier_val(rt_uint8_t data1, rt_uint8_t data2)
{
    humidifier_val = (((data1 + 1) << 8) | data2);
    return 0;
}

//发送温度
rt_uint8_t ab_send_temp(rt_uint8_t data1, rt_uint8_t data2)
{
    raw_pack_init(AB_TEMP, data1, data2);
    hw_send(&ab_tx_buff[0], 6);
    return 0;
}

//发送湿度
rt_uint8_t ab_send_humi(rt_uint8_t data1, rt_uint8_t data2)
{
    raw_pack_init(AB_HUMI, data1, data2);
    hw_send(&ab_tx_buff[0], 6);
    return 0;
}

//发送光照强度
rt_uint8_t ab_send_light(rt_uint8_t data1, rt_uint8_t data2)
{
    raw_pack_init(AB_LIGHT, data1, data2);
    hw_send(&ab_tx_buff[0], 6);
    return 0;
}

rt_uint8_t pack_check_sum(rt_uint8_t * buff)
{
    rt_uint8_t ret = 0;
    if((buff[0] == 0x55) && (buff[5] == 0xAA))
    {
        if((rt_uint8_t)(buff[2]+buff[3]) != buff[4])
        {
            ret = 1;
        }
    }
    else
    {
        ret = 1;
    }
    return ret;
}

//esp8266 发送过来的消息
static AB_Send_MsgEntry abmsgTable[] =
{
    { ESP_VERSION, esp_send_version },
    { ESP_LED, esp_send_led_state },
    { ESP_FAN, esp_send_fan_val },
    { ESP_PTC, esp_send_ptc_val },
    { ESP_HUMIDIFIER, esp_send_humidifier_val },
};

//AB32 上传消息
static AB_Send_MsgEntry msgTable[] =
{
    { AB_TEMP, ab_send_temp },
    { AB_HUMI, ab_send_humi },
    { AB_LIGHT, ab_send_light },
};

//AB32 开始往8266发送消息
void ab_send_msg(rt_uint8_t code, rt_uint8_t data1, rt_uint8_t data2)
{
    int ii = 0;
    CbMsgProc msgProcFunc = 0;
    const int msgNum = sizeof(msgTable) / sizeof(AB_Send_MsgEntry);
    for (ii = 0; ii < msgNum; ii++)
    {
        if(msgTable[ii].cmdCode == code)
        {
            msgProcFunc = msgTable[ii].msgProc;
            break;
        }
    }

    if(msgProcFunc != 0)
    {
        msgProcFunc(data1, data2);
    }
}

//AB32 接收到串口消息进行处理
void AB32_msgProc(rt_uint8_t *rxBuff, int len)
{
    if((len <= AB_RECV_MAX_LEN) && (len > 0))
    {
        memcpy(&ab_rx_buff[0], rxBuff, len);

        if(pack_check_sum(&ab_rx_buff[0]) == 0)
        {
            //校验和成功，查找对应的表
            int ii;
            CbMsgProc msgProcFunc = 0;
            const int msgNum = sizeof(abmsgTable) / sizeof(AB_Send_MsgEntry);
            for (ii = 0; ii < msgNum; ii++)
            {
                if (abmsgTable[ii].cmdCode == ab_rx_buff[1])
                {
                    msgProcFunc = abmsgTable[ii].msgProc;
                    break;
                }
            }

            if (msgProcFunc)
            {
                msgProcFunc(ab_rx_buff[2],ab_rx_buff[3]);
            }
        }
    }
}
